package lt.egidijus.aatest.popup.actions;

import java.util.ArrayList;
import java.util.List;

import lt.egidijus.aatest.generator.TestSuiteGenerator;
import lt.egidijus.aatest.junit.JUnitFinder;
import lt.egidijus.aatest.model.AAtestClass;
import lt.egidijus.aatest.model.AAtestList;
import lt.egidijus.aatest.utils.Constants;
import lt.egidijus.aatest.utils.Utils;
import lt.egidijus.aatest.wizards.RunCustomTestsWizard;
import lt.egidijus.aatest.wizards.helpers.TestsWizardDialog;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IActionDelegate;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;

import com.android.ide.eclipse.adt.internal.launch.junit.AndroidJUnitLaunchShortcut;

public class JunitCustomTestRunnerArction  implements IObjectActionDelegate {

	/**
	 * Constructor for Action1.
	 */
	public JunitCustomTestRunnerArction() {
		super();
	}

	private Shell shell;

	/**
	 * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
	 */
	public void setActivePart(IAction action, IWorkbenchPart targetPart) {
		shell = targetPart.getSite().getShell();
	}
	ISelection lastSelection = null;

	List<ICompilationUnit> list = new ArrayList<ICompilationUnit>(0);

	/**
	 * @see IActionDelegate#run(IAction)
	 */
	public void run(IAction action) {
		if (!Constants.clickTestProject){
			Display.getDefault().asyncExec(new Runnable() {
				public void run() {
					MessageDialog.openError(shell, "Run.. error", "Run can only test in test project!");
				}
			});
			return;
		}
		IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(Constants.projectName);
		JUnitFinder finder = new JUnitFinder();
		ArrayList<ICompilationUnit> cList =  finder.findJUnitsForProject((JavaCore.create(project)));
		AAtestList listtas = new AAtestList();
		listtas.addList(cList);
		IProject project2 = ResourcesPlugin.getWorkspace().getRoot().getProject(Constants.testProjectName);
		ArrayList<ICompilationUnit> cList2 =  finder.findJUnitsForProject((JavaCore.create(project2)));
		listtas.addList(cList2);
		ArrayList<AAtestClass> randomTests = new ArrayList<AAtestClass>();
		for (AAtestClass aClass : listtas.getList()){
			if (aClass.getTestRandom() != null)
				randomTests.add(aClass);
		}
		AAtestList randomList = new AAtestList();
		randomList.setList(randomTests);	
		RunCustomTestsWizard wizard = new RunCustomTestsWizard(shell,randomList);
        TestsWizardDialog dialog = new TestsWizardDialog(shell, wizard);
        dialog.setPageSize(650, 450);
        dialog.create();
        dialog.open();
        if (wizard.getSelectedClassList()!= null){
        	TestSuiteGenerator suiteGen = new TestSuiteGenerator();
 			suiteGen.createTestSuite(wizard.getSelectedClassList());
 			AndroidJUnitLaunchShortcut launch = new AndroidJUnitLaunchShortcut();
 			StructuredSelection select = new StructuredSelection(suiteGen.getGeneratedUnit());
 			launch.launch(select, "run");
        }
	}

	/**
	 * @see IActionDelegate#selectionChanged(IAction, ISelection)
	 */
	public void selectionChanged(IAction action, ISelection selection) {
		Utils.updateProjectsNames(selection);
		if (selection instanceof IStructuredSelection)
				this.lastSelection = selection;

	}
}